package com.zmn.oms.business.impl.conf.workturn.biztype;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.mcc.common.dto.dept.DeptDRO;
import com.zmn.mcc.dubbo.interfaces.dept.DeptListRemoteService;
import com.zmn.oms.business.interfaces.conf.workturn.biztype.PlatBizTypeBService;
import com.zmn.oms.model.entity.conf.workturn.biztype.PlatBizType;
import com.zmn.oms.model.entity.conf.workturn.biztype.log.PlatBizTypeLog;
import com.zmn.oms.services.interfaces.conf.workturn.biztype.PlatBizTypeService;
import com.zmn.oms.services.interfaces.conf.workturn.biztype.log.PlatBizTypeLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class PlatBizTypeBServiceImpl implements PlatBizTypeBService {

    @Resource
    protected PlatBizTypeService platBiztypeService;
    @Resource
    protected PlatBizTypeLogService platBizTypeLogService;
    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    private DeptListRemoteService deptListRemoteService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    @SuppressWarnings("Duplicates")
    public void modifyPlatBizType(List<PlatBizType> platBizTypes, LoginStaff loginStaff) {
        Date now = DateUtil.getNow();
        String operator = loginStaff.getRealName();
        Integer deptId = loginStaff.getDeptId();
        StringBuffer stringBuffer = new StringBuffer();

        platBiztypeService.deleteAllData();
        platBizTypes.forEach(e -> {
            e.setCreater(operator);
            e.setCreateTime(now);
            e.setUpdater(operator);
            e.setUpdateTime(now);
            stringBuffer.append(GlobalConsts.getPlatName(e.getPlat())).append(":").append(this.translateBizTypeEnglishName(e.getBizTypeStr(), "、"))
                    .append("，");
        });

        if (CollectionUtil.isNotNullOrEmpty(platBizTypes)) {
            platBiztypeService.insertBatch(platBizTypes);
        }

        if (stringBuffer.length() > 0) {
            PlatBizTypeLog platBizTypeLog = new PlatBizTypeLog();
            platBizTypeLog.setContent(stringBuffer.deleteCharAt(stringBuffer.length() - 1).toString());
            platBizTypeLog.setCreater(operator);
            platBizTypeLog.setCreateTime(now);
            platBizTypeLog.setUpdater(operator);
            platBizTypeLog.setUpdateTime(now);

            ResponseDTO<DeptDRO> data = deptListRemoteService.getDept(deptId);
            DeptDRO dept = new DeptDRO();
            if (data.isSuccess()) {
                dept = data.getData();
            }
            platBizTypeLog.setDeptId(deptId);
            platBizTypeLog.setDeptName(dept.getDeptName());
            platBizTypeLogService.insert(platBizTypeLog);
        }
    }

    /**
     * 翻译业务类型-英文名称
     *
     * @param bizType
     * @return java.lang.String
     * @author linfeng
     * @modifier
     * @since 2020/8/19
     */
    private String translateBizTypeEnglishName(String bizType, String split) {
        if (StringUtil.isBlank(bizType)) {
            return "";
        }
        if (StringUtil.isBlank(split)) {
            split = "/";
        }
        String splitStr = split;
        List<Integer> bizTypes = StringUtil.convertStrsToList(bizType);
        StringBuilder sb = new StringBuilder();
        bizTypes.forEach(e -> sb.append(this.getBizTypeENName(e)).append(splitStr));
        return sb.deleteCharAt(sb.length() - 1).toString();
    }

    /**
     * 描述: 获取工程师技术等级名称
     *
     * @auther tanghongjun
     * @since 2019/11/20 10:18
     */
    private String getBizTypeENName(int bizType) {
        switch (bizType) {
            case com.zmn.consts.GlobalConsts.BIZ_TYPE_C:
                return "C";
            case com.zmn.consts.GlobalConsts.BIZ_TYPE_F:
                return "F";
            case com.zmn.consts.GlobalConsts.BIZ_TYPE_B:
                return "B";
            default:
                return "";
        }
    }

}
